//
//  YGSickListViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/29.
//

#import "YGSickListViewController.h"
#import "YGOwnHelpBLL.h"
#import "WJItemsControlView.h"
#import "YGSickCollectionViewCell.h"
@interface YGSickListViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) WJItemsControlView  *itemView;
@property (nonatomic, strong) NSArray  *typeArray;
@property (nonatomic, strong) UICollectionView  *collectionView;

@property (nonatomic, strong) NSArray  *dataArray;

@end

@implementation YGSickListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.itemView];
    [self.view addSubview:self.collectionView];
    [self initMas];
    [self requestData];
    // Do any additional setup after loading the view.
}

- (void)initMas{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.itemView.bottom);
        make.left.bottom.right.mas_equalTo(0);
    }];
}
- (void)requestData{
    WeakSelf(self);
    [YGOwnHelpBLL requestDiseaseSuccess:^(NSArray<YGFoodGuideTypeModel *> * _Nonnull model) {
        weakself.typeArray = model;
        NSMutableArray *array = [NSMutableArray array];
        for (YGFoodGuideTypeModel *dmModel in model) {
            [array addObject:dmModel.name];
        }
        weakself.itemView.titleArray = [array copy];
        
        //默认选第一个
        YGFoodGuideTypeModel *yypeModel = [weakself.typeArray objectAtIndex:0];
        [weakself requestParentId:yypeModel.id];


    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)requestParentId:(NSString *)pid{
    WeakSelf(self);
    [YGOwnHelpBLL requestDiseaseID:pid Success:^(NSArray<YGSickListModel *> * _Nonnull model) {
        weakself.dataArray = model;
        [weakself.collectionView reloadData];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YGSickCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YGSickCollectionViewCell" forIndexPath:indexPath];
    YGSickListModel *model =  [self.dataArray objectAtIndex:indexPath.row];
    [cell configureModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YGSickListModel *model =  [self.dataArray objectAtIndex:indexPath.row];
    if (self.ClickBlock) {
        self.ClickBlock(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (WJItemsControlView *)itemView{
    if (!_itemView) {
        WJItemsConfig *config = [[WJItemsConfig alloc]init];
        CGFloat width = self.view.width;
        _itemView = [[WJItemsControlView alloc]initWithFrame:CGRectMake(0,kNavigationHeight, width, 44)];
        _itemView.config = config;
        WeakSelf(self);
//        [_itemView endMoveToIndex:self.itemSelectIndex];
        _itemView.tapItemWithIndex = ^(NSInteger index, BOOL animation) {
            YGFoodGuideTypeModel *model = [weakself.typeArray objectAtIndex:index];
            [weakself requestParentId:model];
        };
    }
    return _itemView;
}

- (UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:YGSickCollectionViewCell.class forCellWithReuseIdentifier:@"YGSickCollectionViewCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
